<?php
namespace App\Service\ApiXcx;

use Illuminate\Support\Facades\DB;

class AddressService extends BaseService
{
    //收货地址

    /**
     * 列表
     * @param int $uid
     */
    public function getList($uid)
    {
        $res = DB::table($this->dbAddress)->where('del', 0)
            ->where('uid', $uid)
            ->get();
        if (!count($res)) {
            return array('code' => 0, 'msg' => '没有地址', 'data' => []);
        }
        $types = $this->addressTypes;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $typeName = array_key_exists($v['type'], $types) ? $types[$v['type']] : '';
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'type_name' => $typeName,
                'accepter' => $v['accepter'],
                'phone' => $v['phone'],
                'wx_door' => $v['wx_door'],
                'wx_map' => $v['wx_map'],
                'selected' => $v['selected'],
                'checked' => $v['checked'],
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 详情
     * @param int $id
     */
    public function getOne($id)
    {
        $res = (array) DB::table($this->dbAddress)->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!count($res)) {
            return array('code' => 0, 'msg' => '地址不存在', 'data' => []);
        }
        $types = $this->addressTypes;
        $typeName = array_key_exists($res['type'], $types) ? $types[$res['type']] : '';
        $arys = [
            'id' => $res['id'],
            'type' => $res['type'],
            'type_name' => $typeName,
            'accepter' => $res['accepter'],
            'phone' => $res['phone'],
            'wx_name' => $res['wx_name'],
            'wx_door' => $res['wx_door'],
            'wx_map' => $res['wx_map'],
            'selected' => $res['selected'],
            'door' => $res['door'],
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    //选项
    public function getOption()
    {
        $arys = [];
        //类型
        $types = $this->addressTypes;
        foreach ($types as $k => $v) {
            $selected = $k==1 ? 1 : 0;
            $arys['type'][] = [
                'id' => $k,
                'title' => $v,
                'selected' => $selected,
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 添加
     * @param int $uid 用户UID
     * @param array $pas 请求参数
     */
    public function add($uid, $pas)
    {
        $pas['uid'] = $uid;
        $pas['addtime'] = $pas['uptime'] = toDate();
        $lastid = DB::table($this->dbAddress)->insertGetId($pas);
        if (!$lastid) {
            return array('code' => -1, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 修改
     * @param int $id 地址ID
     * @param array $pas 请求参数
     */
    public function modify($id, $pas)
    {
        $res = (array) DB::table($this->dbAddress)->where('del', 0)->where('id', $id)->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '地址不存在');
        }
        $pas['uptime'] = toDate();
        $up = DB::table($this->dbAddress)->where('id', $id)->update($pas);
        if (!$up){
            return array('code' => -1, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 修改
     * @param int $id 地址ID
     */
    public function setDel($id)
    {
        $res = (array) DB::table($this->dbAddress)->where('del', 0)->where('id', $id)->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '地址不存在');
        }
        $arys = [
            'del' => 1,
            'deltime' => toDate(),
        ];
        $up = DB::table($this->dbAddress)->where('id', $id)->update($arys);
        if (!$up){
            return array('code' => -1, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 设置默认
     * @param int $id 地址ID
     */
    public function setDefault($id)
    {
        $res = (array) DB::table($this->dbAddress)->where('del', 0)->where('id', $id)->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '地址不存在');
        }
        //取消其他的默认
        $others = [
            'selected' => 0,
            'seltime' => toDate(),
        ];
        $up = DB::table($this->dbAddress)
            ->where('uid', $res['uid'])
            ->where('id', '<>', $id)
            ->update($others);
//        if (!$up){
//            return array('code' => -1, 'msg' => 'other error');
//        }
        //设置默认
        $arys = [
            'selected' => 1,
            'seltime' => toDate(),
        ];
        $up = DB::table($this->dbAddress)->where('id', $id)->update($arys);
        if (!$up){
            return array('code' => -1, 'msg' => 'error');
        }
        return array('code' => 0, 'msg' => 'success');
    }

    /**
     * 获取默认
     * @param int $uid
     */
    public function getDefaultOne($uid)
    {
        $res = (array) DB::table($this->dbAddress)
            ->where('del', 0)
            ->where('uid', $uid)
            ->where('checked', 1)
            ->first();
        if (empty($res)) {
            $res = (array) DB::table($this->dbAddress)
                ->where('del', 0)
                ->where('uid', $uid)
                ->where('selected', 1)
                ->first();
        }
        if (empty($res)) {
            return array('code' => -2, 'msg' => '没有默认地址');
        }
        $arys = [
            'id' => $res['id'],
            'wx_door' => $res['wx_door'] . ' ' . $res['door'],
            'accepter' => $res['accepter'],
            'phone' => $res['phone'],
        ];
        return array('code' => 0, 'msg' => 'success', 'data' => $arys);
    }

    /**
     * 设置选中
     * @param int $uid 用户ID
     * @param int $id 地址ID
     */
    public function setChecked($uid, $id)
    {
        $res = (array) DB::table($this->dbAddress)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return array('code' => -2, 'msg' => '地址不存在');
        }
        //取消其他的选中
        DB::table($this->dbAddress)
            ->where('uid', $uid)
            ->where('id', '<>', $id)
            ->update(['checked' => 0]);
        //设置选中
        DB::table($this->dbAddress)->where('id', $id)->update(['checked' => 1]);
        return array('code' => 0, 'msg' => 'success');
    }
}